package util;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *  基于活动表修补数据通用入参
 * @author zhangweilin
 * @date 2023/3/8 15:09
 * @Description:
 */
@Data
public class DataOpsRepairDataRo<T> implements Serializable {
	private static final long serialVersionUID = -1726639211056834938L;

	/**
	 */
	private Set<Long> param1IdSet;

	/**
	 */
	private Set<String> param2Set;

	/**
	 */
	private Set<String> param3Set;

	/**
	 */
	private Set<String> param4Set;

	/**
	 */
	private Set<String> param5Set;

	/**
	 * 当前id
	 */
	private Long id;


	/**
	 * 批量
	 */
	private Integer batchSize;

	/**
	 * 每一批间隔时间数
	 */
	private Long intervalMs;

	/**
	 * 保留字段
	 */
	private String custom;

	private T data;

	@Slf4j
	public static class LoginParamsUtils {

		private static final ThreadLocal<Map<String, String>> LOGIN_PARAM_LOCAL = ThreadLocal.withInitial(() -> new HashMap<>(10));

		public static final String USER_ID = "operateUserId";

		public static final String USER_NAME = "operateUserName";

		public static final String APP_ID = "appId";

		private LoginParamsUtils(){ }

		/**
		 * 移除LoginParams
		 */
		public static void clearLoginParams(){
			LOGIN_PARAM_LOCAL.remove();
		}

		/**
		 * 获取LoginParams
		 * @return Map<String, String>
		 */
		public static Map<String, String> getLoginParams(){
			return LOGIN_PARAM_LOCAL.get();
		}

		/**
		 * 设置LoginParams（如果有就覆盖，没有就新增）
		 */
		public static void putAllLoginParams(Map<String, String> loginParams){
			if (CollectionUtils.isEmpty(loginParams)){
				return;
			}
			Map<String, String> loginInfoMap = getLoginInfoMap(loginParams);
			getLoginParams().putAll(loginInfoMap);
		}
		public static Map<String, String> getLoginInfoMap() {
			return getLoginInfoMap(getLoginParams());
		}

		public static Map<String, String> getLoginInfoMap(Map<String, String> attachments) {
	//		String userId = attachments.get(util.DataOpsRepairDataRo.LoginParamsUtils.USER_ID);
	//		String userName = attachments.get(util.DataOpsRepairDataRo.LoginParamsUtils.USER_NAME);
	//		String appId = attachments.get(util.DataOpsRepairDataRo.LoginParamsUtils.APP_ID);
	//		Map<String,String> loginInfoMap = new HashMap<>(3);
	//		if (CheckUtil.isNotEmpty(userId)) {
	//			loginInfoMap.put(util.DataOpsRepairDataRo.LoginParamsUtils.USER_ID, userId);
	//		}
	//		if (CheckUtil.isNotEmpty(userName)) {
	//			loginInfoMap.put(util.DataOpsRepairDataRo.LoginParamsUtils.USER_NAME, userName);
	//		}
	//		if (CheckUtil.isNotEmpty(appId)) {
	//			loginInfoMap.put(util.DataOpsRepairDataRo.LoginParamsUtils.APP_ID, appId);
	//		}
	//		return loginInfoMap;

			return null;
		}

		/**
		 * 设置方法
		 * @param key key
		 * @param value value
		 */
		public static void setLoginParam(String key, String value) {
			if (value == null) {
				remove(key);
				return;
			}
			getLoginParams().put(key, value);
		}

		/**
		 * 获取方法
		 * @param key key
		 * @return value
		 */
		public static String getLoginParam(String key) {
			return getLoginParams().get(key);
		}

		public static void remove(String key){
			getLoginParams().remove(key);
		}

		/**
		 * 设置用户ID
		 */
		public static void setUserId(Long userId){
			if (null == userId){
				setLoginParam(USER_ID, null);
				return;
			}
			setLoginParam(USER_ID, String.valueOf(userId));
		}

		/**
		 * 获取用户ID
		 */
		public static Long getUserId(){
			String userIdStr = getLoginParam(USER_ID);
			return (null == userIdStr) ? null : Long.valueOf(userIdStr);
		}

		/**
		 * 设置用户名
		 */
		public static void setUserName(String userName){
			setLoginParam(USER_NAME, userName);
		}

		/**
		 * 获取用户名
		 */
		public static String getUserName(){
			return getLoginParam(USER_NAME);
		}

		/**
		 * 设置appId
		 */
		public static void setAppId(String appId){
			setLoginParam(APP_ID, appId);
		}

		/**
		 * 获取用户名
		 */
		public static String getAppId(){
			return getLoginParam(APP_ID);
		}

	}
}
